<?php 
/**
 * useredit form
 * @author Toan Nguyen
 *
 */

$guid = $vars["entity"]->guid;
$userGuid = elgg_get_logged_in_user_guid();

// Task: get userskill object & skill object
$lstSkillObj = elgg_get_entities(array(
		'type'	=> 'object',
		'subtype'	=> 'skill',
));
$lstUserSkillObj = elgg_get_entities(array(
		'type'	=> 'object',
		'subtype'=>'UserSkill',
		'owner_guid'=>$userGuid,
));

$countSkillObj = count($lstSkillObj);
$countUserSkillObj = count($lstUserSkillObj);

// Task: display userskill(checked) and skill object in a form

$formbody = "<table class='useredit_table'>\n";
$formbody.="<tr>
		<th><h4>".elgg_echo('skill:useredit:yourskills')."</h4></th>
		<th><h4>".elgg_echo('skill:useredit:otherskills')."</h4></th>
		</tr>";
$formbody .= "<tr>";
$formbody .= "<td class='user_skills'>\n";
// fill user skill in the form

foreach ($lstUserSkillObj as $userSkill){
	$skill_name = $userSkill->title;
	$skill_guid = $userSkill->guid;
	$formbody.='<div class="skill_item"><input type="checkbox" checked="checked" name="checkboxes" value="'.$skill_guid.'" >'.$skill_name.'</div>';
}

$formbody .= "</td>";
$formbody .= "<td class='custom_fields_add_form_table_right other_skills'>\n";

// fill remain skill in  the form
foreach ($lstSkillObj as $skillObj){
	if(!isExited($skillObj, $lstUserSkillObj)){
		$skill_name = $skillObj->title;
		$skill_guid = $skillObj->guid;
		$formbody.='<div class="skill_item"><input type="checkbox" name="checkboxes"  value="'.$skill_guid.'" >'.$skill_name.'</div>';
	}
}

$formbody .= "</td>";
$formbody .= "</tr>";
$formbody .= "</table>";

$formbody .= elgg_view("input/hidden", array("name" => "guid", "value" => $guid));
$formbody .= elgg_view("input/hidden", array("name" => 'skill_guids',"id"=>"skill_guids"));

$formbody .= elgg_view('input/submit', array(
		"value" => elgg_echo('skill:save'),
));

// Task: create elgg form
$form = elgg_view('input/form', array(
		'body' => $formbody,
		'action' => $vars['url'] . 'action/skillmanagement/usereditskill',
		'onsubmit'=>"oncheck()"
));


/**
 * Check if a skill is existed in list skill base on title(Name)
 * @param unknown $skill
 * @param unknown $listSkill
 * @return boolean
*/
function isExited($skill, $listSkill){
	foreach ($listSkill as $value){
		if($value->title == $skill->title){
			return true;
		}
	}
	return false;
}

?>


<div class="userskill_form"
	id="custom_fields_profile_type_form">
	<div class="elgg-head">
		<h3>
			<?php echo elgg_echo('skill:useredit:title'); ?>
			<!--<span class='custom_fields_more_info' id='more_info_profile_type'></span>  -->
		</h3>
	</div>
	<div class="elgg-body">
		<?php echo $form; ?>
	</div>
</div>
</form>
<script type="text/javascript">

function onsubmit(){
	alert("Test");
};
/**
 * add and remove skill guid
 */ 
function oncheck(){
	var skill_guidsDOM = document.getElementById("skill_guids"); // DOM element
	var strSkill_guids = ""; 	// list skill guid user has added
	var checkboxes = document.getElementsByName("checkboxes");
	for(var i = 0; i < checkboxes.length; i++){
		if(checkboxes[i].checked){
			strSkill_guids += " " + checkboxes[i].value;
		}
	}
	
	skill_guidsDOM.value = strSkill_guids;
};
	
</script>

<style>
.useredit_table{
	width: 100%;
	padding: 10px;
	margin: 10px;
}
.userskill_form{
	width:500px;
}
.skill_item{
	padding: 5px;
}

.user_skills .other_skills{
}
</style>
